laravel_5_restful_backendfandomcom-20200215-history
Database configuration
Introduction A RESTful backend is an application that provide a way to safely manage a database from third party application. In order to get, manipule, update or delete data from that database. In our example we are going to create a simple database to store some users and some images. Create database Go to your xampp control panel and click the MySql admin button. Welcome to your phpMyAdmin page, i'm not going to run into details here, feel free to check them out on the offical doc. Go to "database", and create a new database. Mine will be named: rest-backend-db That's it, our database is created, we'll use some powerful laravel tools to feed that DB. But first of all, we need to tell laravel how to connect to our database. Let's open the .env file on top of our application folder (Mine: C:\xampp\htdocs\restBackend), and change: DB_HOST=127.0.0.1 to: DB_HOST=localhost * DB_DATABASE=homestead to: (according to your database name) DB_DATABASE=rest-backend-db * DB_USERNAME=homestead to: DB_USERNAME=root * DB_PASSWORD=secret to: DB_PASSWORD= Save it. Now our laravel application can connect to xampp MySql database. Eloquent ORM The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table. Go check their official doc for details. Creating models and tables Laravel already comes with an user model, so we don't have to create one for now. Let's create our image model, lauch GitBash, go to your laravel application folder and execute that simple command: GitBash $ php artisan make:model Images -m * -m for creating the migration file. We can now found our new model (Images.php) in the "app" folder of our application folder. (Mine: C:\xampp\htdocs\restBackend\app\Images.php) And the migration file in "database\migrations" folder. (Mine: C:\xampp\htdocs\restBackend\database\migrations\timestamp_create_images_table.php) Lets open Image.php and edit it as follow: C:\xampp\htdocs\restBackend\app\Images.php increments('id'); $table->string('title', 255); $table->text('description')->nullable(); $table->string('thumbnail')->nullable(); $table->string('imageLink')->nullable(); $table->integer('user_id')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('images'); } } * Here we create the shema of our images table. Table is ready to be create. Run GitBash, go to your application folder and run: GitBash $ php artisan migrate That's it, our tables (Users and Images are created, you can go check out phpMyAdmin). Seeding table with fake datas In order to have some material to work with, we are going to seed our images table with fake data, thanks to laravel seed and faker. GitBash $ php artisan make:seeder ImagesTableSeeder Next open "database\seeds" folder and update it as follow: C:\xampp\htdocs\restBackend\database\seeds\ImagesTableSeeder.php $faker->text(80), 'description' => $content = $faker->paragraph(18), 'thumbnail' => $imageLink.".jpg", 'imageLink' => $imageLink."-l.jpg", 'user_id' => $faker->numberBetween($min = 1, $max = 5), ]); } } } Finaly open in folder and update it as follow: call(ImagesTableSeeder::class); } } Ready to seed our Images table: GitBash $ php artisan db:seed That's it! We have our tables, and some data to work with (go check out with phpMyAdmin). Now let's go to the fun part, the RESTful api! SBenning42 ;)